package com.lms.listener;


import com.lms.dao.BaseDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class DataRollbackThread extends Thread{
    private Timer timer=new Timer();
    @Override
    public void run() {
        String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        try {
            /*24*60*60 * 1000*/
            timer.schedule(new MyTask(), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(format + " 13:35:30"), 24 * 60 * 60 * 1000);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    class MyTask extends TimerTask {
        @Override
        public void run() {
            Connection connection= BaseDao.getConnection();
            PreparedStatement pstm=null;
            String week=getWeek();
            if (connection!=null){
                String sql="update user_apply set apply_state=5 where apply_time_week=?";
                Object[] params={week};
                try {
                    BaseDao.execute(connection,pstm,sql,params);
                } catch (SQLException e) {
                    System.out.println("今日数据更新操作异常！！");
                    e.printStackTrace();
                }
                System.out.println("已执行今日数据更新操作！");
            }else {
                System.out.println("今日数据更新操作异常！！");
            }
        }
        public String getWeek(){
            String[] weekDays = {"sun", "mon", "tue", "wed", "thur", "fri", "sat"};
            Calendar calendar=Calendar.getInstance();
            return weekDays[calendar.get(Calendar.DAY_OF_WEEK)-1];
        }
    }

}
